<template>
  <div class='container'>
    <el-dialog :visible="IsShow" :title="title" width="400px" @close="Handleoff" @open="HandleOn">
      <el-form ref="form" :model="Form" label-width="80px" size="small" :rules="rules">
        <el-form-item label="所属学科 " prop="subjectID" v-if="!$route.query.id||!$route.query.name">
            <el-select v-model="Form.subjectID" placeholder="请选择" style="width:100%">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input
            v-model="Form.directoryName"
            placeholder="请输入学科名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button  @click="Handleoff">取消</el-button>
        <el-button type="primary" @click="handleensure">确认</el-button>
      </template>
    </el-dialog>
    </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { add, update } from '@/api/hmmm/directorys.js'
export default {
  name: 'DirectorysAdd',
  props: {
    IsShow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      options: [], // 下拉框
      Form: {
        subjectID: '', // 学科id
        directoryName: ''// 目录名称
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择学科', trigger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    title () {
      return this.Form.id ? '修改学科' : '添加学科'
    }
  },
  methods: {
    // 关闭弹框
    Handleoff () {
      this.$emit('update:IsShow', false)
      this.$refs.form.resetFields()
      this.Form = {
        subjectID: '', // 学科id
        directoryName: ''// 目录名称
      }
    },
    // 打开弹框时获取下拉框数据
    async HandleOn () {
      const { data } = await simple()
      // console.log(data)
      this.options = data
    },
    // 弹框确认添加/修改
    handleensure () {
      this.$refs.form.validate(async (flag) => {
        if (!flag) return
        if (!this.Form.id) {
          await add(this.Form)
          this.$message.success('添加成功')
        } else {
          await update(this.Form)
          this.$message.success('修改成功')
        }

        this.Handleoff()
        this.$emit('add-handle')
      })
    }

  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-dialog__footer{
  text-align: right;
}
</style>
